package com.basic.java.threads.implementRunnable;

import java.util.concurrent.*;

/**
 * 实现Callable类
 * 创建时间：2022/4/13 23:50
 *
 * @author gejin
 */
public class TestCallable implements Callable<Boolean> {
    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i < 1000; i++) {
            System.out.println(Thread.currentThread().getName() + "--->" + i + "is Running!");
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 实例化对象
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();

        // 创建执行任务
        ExecutorService service = Executors.newFixedThreadPool(3);

        // 提交执行
        Future<Boolean> r1 = service.submit(t1);
        Future<Boolean> r2 = service.submit(t2);
        Future<Boolean> r3 = service.submit(t3);

        // 获取结果
        Boolean resutl1 = r1.get();
        Boolean resutl2 = r2.get();
        Boolean resutl3 = r3.get();

        // 关闭服务
        service.shutdown();
    }
}
